package com.zhike.b2b.advert.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.zhike.b2b.advert.IAdsService;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.model.AdvertVo;

@Service
public class AdsServise extends BaseService implements IAdsService {
    /**
     * 根据广告ID查询广告信息
     * @param adId
     * @return
     */
	public AdvertVo getAd(String adId)
	{
	    String str = "select ad.adv_title,ad.adv_content,ad.adv_url from advertisement ad inner join  adv_site advs on ad.adv_id=advs.trade_id where ad.state_date <=now() and ad.adv_state =1 and advs.trade_id= ? order by ad.oper_time desc limit 0,1";
        AdvertVo adv;
		try {
			adv = jt.queryForObject(str, new RowMapper<AdvertVo>() {
				public AdvertVo mapRow(ResultSet rs, int rowNum) throws SQLException {
					AdvertVo mv = new AdvertVo();
					mv.setTitle(rs.getString("adv_title"));
					String content = rs.getString("adv_content");
					content = content.replace("\\","/");
					mv.setContent(content);
					String url = rs.getString("adv_url");
					if(url.indexOf("http://")==-1){
						url = "http://"+url;
					}
					mv.setUrl(url);
					return mv;
				}
			},adId);
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
			return null;
		}
		return adv;
	}
}
